package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.common.widget.title.TitleView;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public class Listener extends Value {

    public static Listener listener(View.OnClickListener listener) {
        return object(listener);
    }

    public static Listener listener(View.OnLongClickListener listener) {
        return object(listener);
    }

    public static Listener listener(View.OnTouchListener listener) {
        return object(listener);
    }

    public static Listener object(Object listener) {
        return new Listener().accept(my -> my.value = listener);
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {

        if(value(View.OnClickListener.class, listener -> {
            if(helper.getView() instanceof TitleView) {
                ((TitleView) helper.getView()).setNavigationOnClickListener(listener);
            } else {
                helper.setOnClickListener(listener);
            }})) {
            return;
        }

        if(value(View.OnLongClickListener.class, helper::setOnLongClickListener)) {
            return;
        }

        if(value(View.OnTouchListener.class,
                listener -> helper.getViewNulls().doNotNull(
                        view -> view.setOnTouchListener(listener)))) {
            return;
        }

        throw new RuntimeException("No this listener:" + value);
    }
}
